package month1;

public class RemoveNthFromEnd19_04 {
    public static void main(String[] args) {
        System.out.println(removeNthFromEnd(new ListNode(new int[]{1, 2, 3, 4, 5}), 3));
    }

    /**
     * 快慢指针
     * @param head
     * @param n
     * @return
     */
    static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1, head);
        ListNode f = dummy;
        while(n-- > 0 && f != null) {
            f = f.next;
        }
        ListNode s = dummy;
        while(f.next != null) {
            s = s.next;
            f = f.next;
        }
        s.next = s.next.next;
        return dummy.next;
    }
}
